package com.zjdiepu.www.biz.partner;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.enums.type.ThirdModeIDType;
import com.zjdiepu.www.dao.partner.ThCreditResultMapper;
import com.zjdiepu.www.ibiz.partner.IThCreditResultService;
import com.zjdiepu.www.model.partner.dto.ThCreditResult;
import com.zjdiepu.www.model.partner.ext.ThCreditResultExt;

/**
 * 第三方征信结果Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年09月29日 16时24分<br/>
 * @since 1.0<br/>
 */
@Service
public class ThCreditResultServiceImpl extends BaseService implements IThCreditResultService {
	public static final Logger logger = LoggerFactory.getLogger(ThCreditResultServiceImpl.class);

	@Autowired
	private ThCreditResultMapper thCreditResultMapper;
	@Override
	public ThCreditResult saveOrUpdateThCreditResult(String busiId, String busiType, 
			String xeApplyNo, Integer approveStatus, String approveStatusName,
			String peopleName, String peopleNameNo, String carManType, String approveMessage, 
			String currentUserId, String currentUserName) {
		ThirdModeIDType thirdModeIDType = ThirdModeIDType.CSCX; 
		ThCreditResult thCreditResultParam = new ThCreditResult();
		thCreditResultParam.setDpStatus(DpStatus.NORMAL.getCode());
		thCreditResultParam.setBusiId(busiId);
		thCreditResultParam.setThirdModeId(thirdModeIDType.getCode());
		thCreditResultParam.setCarManType(carManType);
		ThCreditResult thCreditResult = thCreditResultMapper.selectRefined(thCreditResultParam);
		boolean insert = false;
		if(null == thCreditResult) {
			insert = true;
			thCreditResult = new ThCreditResult();
			thCreditResult.initInsertData(currentUserId, currentUserName);
		} else {
			thCreditResult.initUpdateData(currentUserId, currentUserName);
		}
		thCreditResult.setBusiType(busiType);
		thCreditResult.setBusiId(busiId);
		thCreditResult.setThirdId(xeApplyNo);
		thCreditResult.setThirdModeId(thirdModeIDType.getCode());
		thCreditResult.setThirdModeName(thirdModeIDType.getValue());
		thCreditResult.setApproveStatus(approveStatus);
		thCreditResult.setApproveStatusName(approveStatusName);
		thCreditResult.setPeopleName(peopleNameNo);
		thCreditResult.setPeopleNameNo(peopleNameNo);
		thCreditResult.setCarManType(carManType);
		thCreditResult.setApproveMessage(approveMessage);
		thCreditResult.setCreditChannelCode("xinhehui-zhengxin");

		if(insert) {
			thCreditResultMapper.insert(thCreditResult);
		} else {
			thCreditResultMapper.updateByPrimaryKeySelective(thCreditResult);
		}
		return thCreditResult;
	}
	
	@Override
	public void saveUpdateAndDelete(ThCreditResult thCreditResult, OperateType operType) {
		switch (operType) {
		case add:
			thCreditResult.initInsertData(thCreditResult.getModifier(), thCreditResult.getModifierName());
			thCreditResultMapper.insert(thCreditResult);
			break;
		case edit:
			thCreditResult.initUpdateData(thCreditResult.getModifier(), thCreditResult.getModifierName());
			// 影响的记录数
			int updateCount = thCreditResultMapper.updateByPrimaryKeySelective(thCreditResult);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			thCreditResult.initDeleteData(thCreditResult.getModifier(), thCreditResult.getModifierName());
			// 影响的记录数
			int deleteCount = thCreditResultMapper.updateByPrimaryKeySelective(thCreditResult);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<ThCreditResult> selectByExt(ThCreditResultExt thCreditResultExt) {
		return thCreditResultMapper.selectByExt(thCreditResultExt);
	}
	
	@Override
	public int countByExt(ThCreditResultExt thCreditResultExt){
		return thCreditResultMapper.countByExt(thCreditResultExt);
	}
	
	@Override
	public ThCreditResult selectById(String id) {
		return thCreditResultMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			thCreditResultMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(ThCreditResult thCreditResult) {
		return thCreditResultMapper.countRefined(thCreditResult);
	}

	@Override
	public ThCreditResult selectRefined(ThCreditResult thCreditResult) {
		return thCreditResultMapper.selectRefined(thCreditResult);
	}
	
	@Override
	public int deletedRefined(ThCreditResult thCreditResult) {
		return thCreditResultMapper.deletedRefined(thCreditResult);
	}

	@Override
	public boolean repeat(ThCreditResult thCreditResult) {
		int count = countRefined(thCreditResult);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(thCreditResult.getId())) {
			return true;
		}
		ThCreditResult dbItem = selectRefined(thCreditResult);
		if(null == dbItem) {
			return false;
		}
		return !thCreditResult.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return thCreditResultMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<ThCreditResult> thCreditResults) {
		return thCreditResultMapper.insertBatch(thCreditResults);
	}
}
